﻿#include "VAOneXMLAdaptorPhysicalPropertyGeneralLaminate.h"

#include "Tools/Win64/rapidxml/rapidxml.hpp"
#include "FITKAcousticsPhysics/FITKPhyscisPropertyGeneralLaminate.h"
#include "FITKAcousticsPhysics/FITKPhyscisPropertyGeneralLaminateIsoLayer.h"
#include "FITK_Kernel/FITKAppFramework/FITKAppFramework.h"
#include "FITK_Kernel/FITKAppFramework/FITKGlobalData.h"
#include "FITKAcousticsPhysics/FITKAcousticsPhysics.h"
#include "FITK_Interface/FITKInterfacePhysics/FITKAbstractMaterial.h"
#include "FITKAcousticsPhysics/FITKAcousticsAbsMaterial.h"
#include "VAOneXMLAdaptorPhysicalPropertyGeneralLaminateIL.h"

#include <QDebug>

namespace IO
{

    bool VAOneXMLAdaptorPhysicalPropertyGeneralLaminate::adaptR()
    {
        return readGeneralLaminate();
    }

    bool VAOneXMLAdaptorPhysicalPropertyGeneralLaminate::adaptW()
    {
        return writeGeneralLaminate();
    }

    bool VAOneXMLAdaptorPhysicalPropertyGeneralLaminate::readGeneralLaminate()
    {
        if(!_node)
        {
            return false;
        }

        bool b = true;
        //转换类型
        Acoustics::FITKPhyscisPropertyGeneralLaminate* glData = dynamic_cast<Acoustics::FITKPhyscisPropertyGeneralLaminate*>(_dataObj);

        if(!glData)
        {
            return false;
        }

        //获取名称节点
        auto nameNode = _node->first_node("Name");

        if (nameNode == nullptr)
        {
            //材料数据中 名称为必要项 不可为空 并抛出异常日志
            consoleMessage(2, "GeneralLaminate name is null");
            return false;
        }

        QString strName = readStrNodeValue(nameNode, &b);

        if(b)
        {
            glData->setDataObjectName(strName);//名称可以为空，但是节点不能为空
        }


        auto noteNode = _node->first_node("Note");
        if (noteNode != nullptr)
        {
            QString strNote = readStrNodeValue(noteNode, &b);
            if(b)
            {
                glData->setDescribe(strNote);
            }
        }

        //获取颜色节点
        auto colorNode = _node->first_node("Color");
        float colorR = 0; float colorG = 0; float colorB = 0;
        //根据节点获取颜色
        readColorNodeValue(colorNode, colorR, colorG, colorB);
        //设置颜色
        glData->setColor(colorR, colorG, colorB);
        readGeneralLaminateIsoLayer();
        return true;
    }

    bool VAOneXMLAdaptorPhysicalPropertyGeneralLaminate::writeGeneralLaminate()
    {

        if (!_node)// 判断节点是否存在
        {
            return false;
        }

        //转换类型
        Acoustics::FITKPhyscisPropertyGeneralLaminate* data = dynamic_cast<Acoustics::FITKPhyscisPropertyGeneralLaminate*>(_dataObj);

        if(!data)
        {
            return false;
        }

        if(!_node)
        {
            return false;
        }

        // 创建名称节点
        createNodeElement(_node, "Name", data->getDataObjectName());
        // 创建备注节点
        createNodeElement(_node, "Note", data->getDescribe());
        //创建颜色节点
        auto colorNode = createNodeElement(_node, "Color");

        //写入颜色节点信息
        if(colorNode)
        {
            float rgb[3] = {0.1f, 0.2f, 0.3f};

            data->getColor(rgb);
            writeAttribute(colorNode, "colorR", rgb[0]);
            writeAttribute(colorNode, "colorG", rgb[1]);
            writeAttribute(colorNode, "colorB", rgb[2]);
        }

        writeGeneralLaminateIsoLayer();
        return true;
    }

    bool VAOneXMLAdaptorPhysicalPropertyGeneralLaminate::readGeneralLaminateIsoLayer()
    {
        auto layerNode = _node->first_node("IsotropicLayer");

        if (!layerNode)// 判断Fluid节点是否存在
        {
            return false;
        }

        bool b = true;
        //转换类型为自由阻尼合版
        Acoustics::FITKPhyscisPropertyGeneralLaminate* GLData = dynamic_cast<Acoustics::FITKPhyscisPropertyGeneralLaminate*>(_dataObj);
        int index = 0;//记录索引

        //创建读取操作器对象
        IO:: VAOneXMLAdaptorPhysicalPropertyGeneralLaminateIL* adaptor{};
        // 循环处理每个layer节点数据
        while (layerNode)
        {
            //创建数据类对象
            Acoustics::FITKPhyscisPropertyGeneralLaminateIsoLayer* layerData = new Acoustics::FITKPhyscisPropertyGeneralLaminateIsoLayer;
            if(!adaptor)
            {
                adaptor = FITKIOADAPTORFACTORY->createT<IO::VAOneXMLAdaptorPhysicalPropertyGeneralLaminateIL>("VAOneXML", layerData);
            }

            if(!adaptor)
            {
                //读写器创建异常
                consoleMessage(3,"create VAOneXMLAdaptorPhysicalPropertyGeneralLaminateIsoLayer Error");
                return false;
            }

            adaptor->setNode(layerNode);
            adaptor->setDataObject(layerData);
            b &= adaptor->adaptR();
            //在这里判断是否存在读取的层信息
            auto layerDataOld = GLData->getDataByIndex(index);

            if(!layerDataOld)//如果为空，则插入到数据类中
            {
                GLData->insertDataObj(index, layerData);
            }
            else//不为空，则复制当前数据类
            {
                layerDataOld->copy(layerData);
                delete layerData;//删除创建的数据
            }

            layerNode = layerNode->next_sibling("IsotropicLayer");
            index++;
        }

        delete adaptor;
        adaptor = nullptr;
        //读取成功
        return b;
    }

    bool VAOneXMLAdaptorPhysicalPropertyGeneralLaminate::writeGeneralLaminateIsoLayer()
    {
        //转换类型
        Acoustics::FITKPhyscisPropertyGeneralLaminate* data = dynamic_cast<Acoustics::FITKPhyscisPropertyGeneralLaminate*>(_dataObj);//上一个函数已经判空
        bool b = true;
        //获取层数量
        int dataCount = data->getDataCount();
        IO:: VAOneXMLAdaptorPhysicalPropertyGeneralLaminateIL* adaptor{};
        for(int i = 0; i < dataCount ; i++)
        {
            //创建节点
            rapidxml::xml_node<char>* node = createNodeElement(_node, "IsotropicLayer");

            if(!node)
            {
                return false;
            }

            auto layerData = data->getDataByIndex(i);

            if(!layerData)
            {
                continue;
            }

            if(!adaptor)
            {
                adaptor = FITKIOADAPTORFACTORY->createT<IO::VAOneXMLAdaptorPhysicalPropertyGeneralLaminateIL>("VAOneXML", layerData);
            }

            if(!adaptor)
            {
                //读写器创建异常
                consoleMessage(3,"create adaptor Error");
                return false;
            }

            adaptor->setNode(node);
            adaptor->setDataObject(layerData);
            b &= adaptor->adaptW();
        }

        delete adaptor;
        adaptor = nullptr;
        return b;
    }
}
